// const app = getApp();
const path ='https://zhuomei.doing.net.cn/Xcxapi/';
// const path ='http://shuiguo3.com/Xcxapi/';
const handleRequest = (request, options, resolve, reject) => {
  if (request.data.errcode === 1) {
    if (options.message) {
      wx.showToast({
        title: request.data.message,
        icon: "none"
      })
    }
    resolve(request.data);
  } else if (request.data.errcode==100 || request.data.errcode==101) {
    wx.showToast({
      title: "请先登录！",
      icon: "none"
    })
    setTimeout(() => {
      wx.switchTab({
        url: '/pages/my/my',
        success:function(){
          wx.removeStorageSync('token');
          wx.removeStorageSync('userInfo');
        }
      })
    }, 1000)
  } else {
    wx.showToast({
      title: request.data.message,
      icon: "none"
    })
    reject(request.data)
  }
}
const request = (url, options) => {
  return new Promise((resolve, reject) => {
    options.message = options.message === undefined ? true : options.message
    if (options.upload) {
      wx.uploadFile({
        url: path + url,
        filePath: options.data[options.upload.name],
        name: options.upload.name,
        formData: {
          token: options.data.token
        },
        success(request) {
          request.data = JSON.parse(request.data)
          handleRequest(request, options, resolve, reject)
        },
        fail(error) {
          wx.showToast({
            title: '网络异常！',
            icon: "none"
          })
          reject(error.data);
        }
      })
    } else {
      wx.request({
        url:path+url,//获取域名接口地址
        method: options.method || "POST", //配置method方法
        data: options.data || {},
        header: {
          'content-Type': 'application/x-www-form-urlencoded',        
        },
        success(request) {
        //监听成功后的操作
          // console.log(url);
          // console.log(request);
          if (options.status1) {
            handleRequest(request, options, resolve, reject)
          } else {
            resolve(request.data);
            if(request.data.errcode==100 || request.data.errcode==101){//-100 登录超时,请重新登录 101 您的账号已在其它设备登录，系统自动退出。
              // wx.navigateTo({
              //   url: '/pages/login/login',
              //   success:function(){
              //     wx.removeStorageSync('token');
              //     wx.removeStorageSync('userInfo');
              //   }
              // })
              setTimeout(() => {
                wx.switchTab({
                  url: '/pages/my/my',
                  success:function(){
                    wx.removeStorageSync('token');
                    wx.removeStorageSync('userInfo');
                  }
                })
              }, 1000)
            }
          }
        },
        fail(error) {
          // console.log(url);
          // console.log(error);
          wx.showToast({
            title: '网络异常！',
            icon: "none"
          })
          reject(error.data);
        }
      })
    }
  })
}
export default request;